package com.rtx.wms.std.po.service.impl;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rtx.api.annotation.DataSourceAnnotation;
import com.rtx.wms.std.asn.bean.Receipt;
import com.rtx.wms.std.asn.bean.Receiptdetail;
import com.rtx.wms.std.asn.mapper.ReceiptMapper;
import com.rtx.wms.std.asn.mapper.ReceiptdetailMapper;
import com.rtx.wms.std.po.bean.ApiPo;
import com.rtx.wms.std.po.bean.ApiPodetail;
import com.rtx.wms.std.po.bean.Po;
import com.rtx.wms.std.po.bean.Podetail;
import com.rtx.wms.std.po.bean.RtxCarton;
import com.rtx.wms.std.po.mapper.PoMapper;
import com.rtx.wms.std.po.mapper.PodetailMapper;
import com.rtx.wms.std.po.mapper.RtxCartonMapper;
import com.rtx.wms.std.po.service.PoService;

@Service
public class PoServiceImpl extends ServiceImpl<PoMapper, Po> implements PoService{

	@Autowired
	ReceiptMapper receiptMapper;
	
	@Autowired
	ReceiptdetailMapper receiptdetailMapper;

	@Autowired
	PodetailMapper podetailMapper;

	@Autowired
	RtxCartonMapper rtxCartonMapper;

	@DataSourceAnnotation
	@Override
	public List<Po> queryByExternpokey(String datasourckey, String externpokey) {
		LambdaQueryWrapper<Po> qw = Wrappers.lambdaQuery();
		qw.eq(Po::getExternpokey, externpokey);
		return baseMapper.selectList(qw);
	}

	@Transactional
	@DataSourceAnnotation
	@Override
	public void deletePoAndAsn(String datasourckey, String pokey,String receiptkey,String externpokey) {
		LambdaQueryWrapper<Receiptdetail> rdqw =Wrappers.lambdaQuery();
		rdqw.eq(Receiptdetail::getReceiptkey, receiptkey);
		receiptdetailMapper.delete(rdqw);
		
		LambdaQueryWrapper<Receipt> qw = Wrappers.lambdaQuery();
		qw.eq(Receipt::getReceiptkey, receiptkey);
		receiptMapper.delete(qw);

		LambdaQueryWrapper<Po> poqw = Wrappers.lambdaQuery();
		poqw.eq(Po::getPokey, pokey);
		baseMapper.delete(poqw);

		LambdaQueryWrapper<Podetail> podetailqw = Wrappers.lambdaQuery();
		podetailqw.eq(Podetail::getPokey, pokey);
		podetailMapper.delete(podetailqw);

		LambdaQueryWrapper<RtxCarton> carton = Wrappers.lambdaQuery();
		carton.eq(RtxCarton::getExternpokey, externpokey);
		rtxCartonMapper.delete(carton);
	}

	@Transactional
	@DataSourceAnnotation
	@Override
	public void deletePoAndDetail(String datasourckey, String pokey,String externpokey) {
		LambdaQueryWrapper<Po> poqw = Wrappers.lambdaQuery();
		poqw.eq(Po::getPokey, pokey);
		baseMapper.delete(poqw);

		LambdaQueryWrapper<Podetail> podetailqw = Wrappers.lambdaQuery();
		podetailqw.eq(Podetail::getPokey, pokey);
		podetailMapper.delete(podetailqw);

		LambdaQueryWrapper<RtxCarton> carton = Wrappers.lambdaQuery();
		carton.eq(RtxCarton::getExternpokey, externpokey);
		rtxCartonMapper.delete(carton);
	}

	@Transactional
	@DataSourceAnnotation
	@Override
	public void updPoAndPodetail(String datasourckey, Po po,ApiPo apipo, List<Podetail> dList) {
		LambdaQueryWrapper<Po> poupd = Wrappers.lambdaQuery();
		poupd.eq(Po::getPokey, po.getPokey());
		
		po.setRtxErptype(apipo.getRtxErptype());
		po.setRtxdemandtype(apipo.getRtxdemandtype());
		po.setSuppliercode(apipo.getSuppliercode());
		po.setSuppliername(apipo.getSuppliername());
		po.setShipfromaddressline1(apipo.getShipfromaddressline1());
		po.setShipfromcontact(apipo.getShipfromcontact());
		po.setRtxdepartment(apipo.getRtxdepartment());
		po.setRtxwerks(apipo.getRtxwerks());
		po.setToexamine(apipo.getToexamine());
		po.setRtxqcmark(apipo.getRtxqcmark());
		po.setNotes(apipo.getNotes());
		po.setRtxneima(apipo.getRtxneima());
		baseMapper.update(po, poupd);

		for(Podetail pd:dList) {
			LambdaUpdateWrapper<Podetail> pdUpd = Wrappers.lambdaUpdate();
			pdUpd.eq(Podetail::getPokey, pd.getPokey());
			pdUpd.eq(Podetail::getPolinenumber, pd.getPolinenumber());
			pdUpd.eq(Podetail::getSku, pd.getSku());
			pdUpd.set(Podetail::getQtyordered,pd.getQtyordered());
			pdUpd.set(Podetail::getMarkscontainer,pd.getMarkscontainer());
			pdUpd.set(Podetail::getSkudescription,pd.getSkudescription());
			pdUpd.set(Podetail::getManufacturersku,pd.getManufacturersku());
			pdUpd.set(Podetail::getRetailsku, pd.getRetailsku());
			pdUpd.set(Podetail::getAltsku, pd.getAltsku());
			pdUpd.set(Podetail::getPackkey, "STD");
			pdUpd.set(Podetail::getUom, "EA");
			pdUpd.set(StringUtils.hasLength(pd.getForte_flag()),Podetail::getForte_flag, pd.getForte_flag());
			pdUpd.set(Podetail::getSusr1, pd.getSusr1());
			pdUpd.set(Podetail::getSusr2, pd.getSusr2());
			pdUpd.set(Podetail::getSusr3, pd.getSusr3());
			pdUpd.set(Podetail::getSusr4, pd.getSusr4());
			pdUpd.set(Podetail::getSusr5, pd.getSusr5());
			pdUpd.set(Podetail::getQcautoadjust, pd.getQcautoadjust());
			pdUpd.set(Podetail::getQcrequired, pd.getQcrequired());
			pdUpd.set(Podetail::getSourcelocation, pd.getSourcelocation());
			pdUpd.set(Podetail::getSourceversion, pd.getSourceversion());
			pdUpd.set(Podetail::getRtxstylekey, pd.getRtxstylekey());
			pdUpd.set(Podetail::getToloc, pd.getToloc());
			pdUpd.set(Podetail::getReceiptlinenumber, pd.getReceiptlinenumber());
			pdUpd.set(Podetail::getRtxsizekey, pd.getRtxsizekey());
			pdUpd.set(Podetail::getRtxcolorkey, pd.getRtxcolorkey());
			pdUpd.set(Podetail::getExterpokeyb2creturn, pd.getExterpokeyb2creturn());
			pdUpd.set(Podetail::getB2cpokey, pd.getB2cpokey());
			pdUpd.set(Podetail::getB2creturnno, pd.getB2creturnno());
			pdUpd.set(Podetail::getRollno, pd.getRollno());
			pdUpd.set(Podetail::getVatno, pd.getVatno());
			pdUpd.set(Podetail::getMaterialbreadth, pd.getMaterialbreadth());
			pdUpd.set(Podetail::getTofactory, pd.getTofactory());
			pdUpd.set(Podetail::getRtxwerks, pd.getRtxwerks());
			pdUpd.set(Podetail::getRtxlgort, pd.getRtxlgort());
			pdUpd.set(Podetail::getToexamine, pd.getToexamine());
			pdUpd.set(Podetail::getRtxqcmark, pd.getRtxqcmark());
			pdUpd.set(Podetail::getRtx_erpokey, pd.getRtx_erpokey());
			pdUpd.set(Podetail::getRtxfromwerks, pd.getRtxfromwerks());
			pdUpd.set(Podetail::getRtxfromlgort, pd.getRtxfromlgort());
			pdUpd.set(Podetail::getRtxerplot, pd.getRtxerplot());
			pdUpd.set(Podetail::getRtxpd, pd.getRtxpd());
			pdUpd.set(Podetail::getNotes, pd.getNotes());
			podetailMapper.update(null, pdUpd);
		}
	}

	@Transactional
	@DataSourceAnnotation
	@Override
	public void addPoAndDetailAndCarton(String datasourckey, Po po) {
		for(RtxCarton carton:po.getCartonList()) {
			rtxCartonMapper.insert(carton);
		}

		for(Podetail podetail:po.getPodetailList()) {
			podetailMapper.insert(podetail);
		}

		this.save(po);
	}

	//新增podetail行项
	@DataSourceAnnotation
	@Override
	public void addExistsPodetail(String datasourckey, Po po, List<ApiPodetail> dList,ApiPo apo) {
		LambdaQueryWrapper<Podetail> podetailqw = Wrappers.lambdaQuery();
		podetailqw.eq(Podetail::getPokey, po.getPokey());
		List<Podetail> podetailList = podetailMapper.selectList(podetailqw);
		int nextlineno = podetailList.size()+1;

		for(ApiPodetail apd:dList) {
			Podetail pd = new Podetail();
			BeanUtils.copyProperties(apd,pd);
			pd.setPokey(po.getPokey());
			pd.setPolinenumber(String.format("%05d", nextlineno++));
			pd.setToexamine(apo.getToexamine());
			pd.setRtxqcmark(apo.getRtxqcmark());
			pd.setRtxwerks(apo.getRtxwerks());
			pd.setStorerkey("GREPOW");
			pd.setStatus("0");
			pd.setQtyreceived("0");
			pd.setExternpokey(po.getExternpokey());
			pd.setPackkey("STD");
			pd.setUom("EA");
			pd.setRtx_erpokey(apd.getRtxErpokey());
			podetailMapper.insert(pd);
		}

	}

	@Transactional
	@DataSourceAnnotation
	@Override
	public void updPodetailStatus(String datasourckey, String poKey, String status) {
		LambdaUpdateWrapper<Podetail> pdUpd = Wrappers.lambdaUpdate();
		pdUpd.eq(Podetail::getPokey, poKey);
		pdUpd.set(Podetail::getStatus, status);
		podetailMapper.update(null, pdUpd);
	}
	
	@Transactional
	@DataSourceAnnotation
	@Override
	public void updPoStatus(String datasourckey, String poKey, String status) {
		LambdaUpdateWrapper<Po> poupd = Wrappers.lambdaUpdate();
		poupd.eq(Po::getPokey, poKey);
		poupd.set(Po::getStatus, status);
		baseMapper.update(null, poupd);
	}
}
